package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.CnasNewMapper;
import com.zhijian.medical.entity.po.CnasNew;
import com.zhijian.medical.entity.vo.request.CnasNewSearchVo;
import com.zhijian.medical.entity.vo.request.CnasNewSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.response.CnasNewVo;
import com.zhijian.medical.service.CnasNewService;
import com.zhijian.util.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.util.AssertUtil;
import org.springframework.beans.BeanUtils;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * cnas相关内容业务处理层
 * 
 * @author auto
 * @version: 1.0
 * @date 2025-06-17 10:45:42
 */
@Slf4j
@Service
public class CnasNewServiceImpl implements CnasNewService {

	@Autowired
    private CnasNewMapper mapper;

    @Override
    public CnasNew selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CnasNewVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(CnasNew cnasNew) {
        Date now = new Date();
        cnasNew.setCreateTime(now);
        cnasNew.setUpdateTime(now);
        return mapper.insert(cnasNew) > 0 ? cnasNew.getId() : 0L;
    }

    @Override
    public Long insertSelective(CnasNew cnasNew) {
        Date now = new Date();
        cnasNew.setCreateTime(now);
        cnasNew.setUpdateTime(now);
        return mapper.insertSelective(cnasNew) > 0 ? cnasNew.getId() : 0L;
    }

    @Override
    public boolean update(CnasNew cnasNew) {
        Date now = new Date();
        cnasNew.setUpdateTime(now);
        return mapper.updateByPrimaryKey(cnasNew) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CnasNew record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(CnasNewSaveOrUpdateVo saveOrUpdateVo) {
        CnasNew cnasNew = new CnasNew();
        BeanUtils.copyProperties(saveOrUpdateVo, cnasNew);
        return this.insert(cnasNew) != 0;
    }

    @Override
    public boolean modify(CnasNewSaveOrUpdateVo saveOrUpdateVo) {
        CnasNew old = this.selectById(saveOrUpdateVo.getId());
        AssertUtil.notNull(old, "此数据不存在或者已被删除");
        CnasNew cnasNew = new CnasNew();
        BeanUtils.copyProperties(saveOrUpdateVo, cnasNew);
        // 组装内容字段
        Byte sopType = saveOrUpdateVo.getSopType();
        String reviewContent = saveOrUpdateVo.getReviewContent();
        Byte reviewType = saveOrUpdateVo.getReviewType();
        Byte resultType = saveOrUpdateVo.getResultType();
        if (Objects.nonNull(sopType)) {
            switch (sopType.intValue()) {
                case 1:
                    cnasNew.setReviewContentOne(reviewContent);
                    cnasNew.setReviewTypeOne(reviewType);
                    cnasNew.setResultTypeOne(resultType);
                    break;
                case 2:
                    cnasNew.setReviewContentTwo(reviewContent);
                    cnasNew.setReviewTypeTwo(reviewType);
                    cnasNew.setResultTypeTwo(resultType);
                    break;
                case 3:
                    cnasNew.setReviewContentThree(reviewContent);
                    cnasNew.setReviewTypeThree(reviewType);
                    cnasNew.setResultTypeThree(resultType);
                    break;
                case 4:
                    cnasNew.setReviewContentFour(reviewContent);
                    cnasNew.setReviewTypeFour(reviewType);
                    cnasNew.setResultTypeFour(resultType);
                    break;
                case 5:
                    cnasNew.setReviewContentFive(reviewContent);
                    cnasNew.setReviewTypeFive(reviewType);
                    cnasNew.setResultTypeFive(resultType);
                    break;
                case 6:
                    cnasNew.setReviewContentSix(reviewContent);
                    cnasNew.setReviewTypeSix(reviewType);
                    cnasNew.setResultTypeSix(resultType);
                    break;
                default:
                    cnasNew.setReviewContent(reviewContent);
            }
        }
        return this.updateByPrimaryKeySelective(cnasNew);
    }

    @Override
    public CnasNewVo selectOne(CnasNewSearchVo cnasNewSearchVo) {
        return mapper.selectOne(cnasNewSearchVo);
     }

    @Override
    public CnasNewSaveOrUpdateVo.ReviewContent selectContent(Long id, Byte sopType) {
        return mapper.selectContent(id, sopType);
    }

    @Override
    public Integer count(CnasNewSearchVo cnasNewSearchVo) {
        return mapper.count(cnasNewSearchVo);
    }

    @Override
    public List<CnasNewVo> selectList(CnasNewSearchVo cnasNewSearchVo) {
        return mapper.selectList(cnasNewSearchVo);
    }

    @Override
    public Page<CnasNewVo> selectPage(CnasNewSearchVo cnasNewSearchVo) {
        AssertUtil.check(Objects.isNull(cnasNewSearchVo.getPageNum()) || Objects.isNull(cnasNewSearchVo.getPageSize()), "分页参数不能为空");
        //先直接查总数
        int count = count(cnasNewSearchVo);
        //分页
        Page<CnasNewVo> page = new Page<>(true, count, cnasNewSearchVo.getPageNum(), cnasNewSearchVo.getPageSize());
        cnasNewSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CnasNewVo> responseVos = this.selectList(cnasNewSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CnasNew> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(CnasNewSearchVo cnasNewSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(cnasNewSearchVo);
    }
    
}